<?php
// +----------------------------------------------------------------------
// | 用户后台基础类--CMS分组PUBLIC公共类
// +----------------------------------------------------------------------
// | YstechCMS V1.0 Beta
// +----------------------------------------------------------------------
// | Copyright (c) 2014 http://www.YstechCMS.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: Ystech <2094157689@qq.com>
// +----------------------------------------------------------------------
namespace Cms\Controller;
use Cms\Controller\BaseController;
class PublicController extends BaseController {
	
	//默认跳转至登陆页面
	public function index(){
		$this->redirect(U('/Cms/Public/login'));
	}	
	
	//通用注册页面
	public function reg(){
		$this->display();
	}
	
	//通用登陆页面
    public function login(){
    	if(IS_POST){
    		$data=I('post.');
			$verify = new \Think\Verify();
    		if(!$verify->check($data['verify'])){
    			$this->error('请正确填写验证码！',u('public/login'));
    		}
			$user=M('User')->where(array('username'=>$data['username'],'userpass'=>md5($data['userpass'])))->find();
			//dump($user); ["oath"] => string(17) "vip,log,adminlog,"
			//die;
    		if($user){
				$nodeList = M('role_node','basis_')
						->field('basis_node.*,basis_role_node.admin,basis_role_node.read,basis_role_node.write')
						->join('basis_role_user on basis_role_user.role_id=basis_role_node.role_id')
						->join('basis_node on basis_node.id=basis_role_node.node_id')
						->where('basis_role_user.user_id='.$user['id'].' and basis_node.is_del=0 and basis_node.type_id=1')
						->select();
				$nodeList = list_to_tree($nodeList);
				$functionList =  M('role_node','basis_')
						->join('basis_role_user on basis_role_user.role_id=basis_role_node.role_id')
						->join('basis_node on basis_node.id=basis_role_node.node_id')
						->where('basis_role_user.user_id='.$user['id'].' and basis_node.is_del=0 and basis_node.type_id=2')
						->getField('basis_node.*,basis_role_node.admin,basis_role_node.read,basis_role_node.write');
				self::$CMS['nodeList']=$_SESSION['CMS']['nodeList']=$nodeList;
				self::$CMS['nodeList']=$_SESSION['CMS']['functionList']=$functionList;
    			self::$CMS['uid']=$_SESSION['CMS']['uid']=$user['id'];
				self::$CMS['user']=$_SESSION['CMS']['user']=$user;
				self::$CMS['homeurl']=$_SESSION['CMS']['homeurl']=U('/Cms/Index/index');
				self::$CMS['backurl']=$_SESSION['CMS']['backurl']=FALSE;
				//追入操作员日志
				$adlog['uid']=$_SESSION['CMS']['uid'];
				$adlog['admin']=$_SESSION['CMS']['user']['username'];
				$adlog['ip']=get_client_ip();
				$adlog['ctime']=time();
				$adlog['event']='登入';
				$radlog=M('Adminlog_login')->add($adlog);
    			$this->redirect(U('/Cms/Index/index'));
    		}else{
    			$this->error('用户不存在，或密码错误！');
    		}
		}
		if($_SESSION['CMS']['uid']){
			$this->redirect(U('/Cms/Index/index'));
		}
    	$this->display();    
	}
	
	public function logout(){
		//追入操作员日志
		$adlog['uid']=$_SESSION['CMS']['uid'];
		$adlog['admin']=$_SESSION['CMS']['user']['username'];
		$adlog['ip']=get_client_ip();
		$adlog['ctime']=time();
		$adlog['event']='登出';
		$radlog=M('Adminlog_login')->add($adlog);
		session(null);
		$this->redirect(U('/Cms/Public/login'));			
	}
	
	//通用验证码
	public function verify(){
    	$Verify = new \Think\Verify();
		$Verify->codeSet = '0123456789'; 
		$Verify->length   = 4;
		$Verify->imageH   = 0;
		$Verify->entry();
 	}
	
	//百度地图
	public function baiduDitu(){
		$map['address']=I('address');
		$map['lng']=I('lng');
		$map['lat']=I('lat');
		$this->assign('map',$map);
		$mb=$this->fetch();
		$this->ajaxReturn($mb);
	}
	
	
}